package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;

public class RecyclerViewActivity extends AppCompatActivity {

    private List<Fruit> fruitList = new ArrayList<Fruit>();
    private RecyclerView mRv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recyclear_view_demo);

        initView();
        initFruit();

    }

    private void initView() {
        //找到对应的组件 绑定组件
        mRv = (RecyclerView)findViewById(R.id.recycler_view);
        //设置布局管理方式 布局管理方式的使用和应用
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        //设置布局的排列方式
        linearLayoutManager.setInitialPrefetchItemCount(LinearLayoutManager.VERTICAL);
        //设置适配器

//        mRv.setAdapter(adapter);
        mRv.setLayoutManager(linearLayoutManager);

        MyRecyclerAdapter adapter = new MyRecyclerAdapter(fruitList,R.layout.fruit_item);
        mRv.setAdapter(adapter);
    }

    //初始化水果数据
    private void initFruit() {
        for(int i=0;i<2;i++){
            Fruit apple = new Fruit("Apple",R.drawable.apple_pic);
            fruitList.add(apple);
            Fruit banana = new Fruit("Banana",R.drawable.banana_pic);
            fruitList.add(banana);
            Fruit orange = new Fruit("Orange",R.drawable.orange_pic);
            fruitList.add(orange);
            Fruit pear = new Fruit("Pear",R.drawable.pears_pic);
            fruitList.add(pear);
            Fruit pineapple = new Fruit("Pineapple",R.drawable.pineapple_pic);
            fruitList.add(pineapple);
            Fruit strawberry = new Fruit("Strawberry",R.drawable.strawberry_pic);
            fruitList.add(strawberry);
            Fruit tomato = new Fruit("Tomato",R.drawable.tomato_pic);
            fruitList.add(tomato);
            Fruit watermelon = new Fruit("Watermelon",R.drawable.watermelon_pic);
            fruitList.add(watermelon);
            fruitList.add(strawberry);
            Fruit tomato1= new Fruit("Tomato",R.drawable.tomato_pic);
            fruitList.add(tomato1);
            Fruit watermelon1 = new Fruit("Watermelon",R.drawable.watermelon_pic);
            fruitList.add(watermelon1);
        }
    }

}